<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Js 封装二叉搜索树 | Saturday_</title><meta name="keywords" content="js 封装二叉搜索树"><meta name="author" content="HHR"><meta name="copyright" content="HHR"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Js 封装二叉搜索树">
<meta property="og:type" content="article">
<meta property="og:title" content="Js 封装二叉搜索树">
<meta property="og:url" content="https://codehhr.cn/2021/10/19/data-structure/ds-binarySearchTree/index.html">
<meta property="og:site_name" content="Saturday_">
<meta property="og:description" content="Js 封装二叉搜索树">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg">
<meta property="article:published_time" content="2021-10-19T13:51:50.000Z">
<meta property="article:modified_time" content="2021-10-29T14:33:04.912Z">
<meta property="article:author" content="HHR">
<meta property="article:tag" content="数据结构">
<meta property="article:tag" content="二叉树">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="https://codehhr.cn/2021/10/19/data-structure/ds-binarySearchTree/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: {"limitDay":680,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: HHR","link":"链接: ","source":"来源: Saturday_","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Js 封装二叉搜索树',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-10-29 22:33:04'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="/js/jquery.min.js"></script><link rel="stylesheet" href="/css/cursor.css"><link rel="stylesheet" href="/css/footergradient.css"><link rel="stylesheet" href="/css/scrollbar.css"><link rel="stylesheet" href="/css/animate.min.css"><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Saturday_" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">87</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">120</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-image"></i><span> 照片</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/school/"><i class="fa-fw fas fa-graduation-cap"></i><span> 科大</span></a></li><li><a class="site-page child" href="/wallpaper/"><i class="fa-fw fas fa-image"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/csslayouts/sunrise.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Saturday_</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-image"></i><span> 照片</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/school/"><i class="fa-fw fas fa-graduation-cap"></i><span> 科大</span></a></li><li><a class="site-page child" href="/wallpaper/"><i class="fa-fw fas fa-image"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Js 封装二叉搜索树</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-19T13:51:50.000Z" title="发表于 2021-10-19 21:51:50">2021-10-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-10-29T14:33:04.912Z" title="更新于 2021-10-29 22:33:04">2021-10-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%A0%91%E7%BB%93%E6%9E%84/">树结构</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>19分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Js 封装二叉搜索树"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><div class="note info no-icon flat"><h1 id="二叉搜索树的封装"><a href="#二叉搜索树的封装" class="headerlink" title="二叉搜索树的封装"></a>二叉搜索树的封装</h1></div>

<h1 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h1><div class="hide-toggle" ><div class="hide-button toggle-title" style=""><i class="fas fa-caret-right fa-fw"></i><span>code</span></div>
    <div class="hide-content"><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 二叉搜索树的封装</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">BinarySearchTree</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 节点类 (每个节点都包含左右两个子节点以及自身的 key &amp; value )</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">Node</span>(<span class="params">obj = &#123;&#125;</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.key = obj.key;</span><br><span class="line">    <span class="built_in">this</span>.value = obj.value;</span><br><span class="line">    <span class="built_in">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="built_in">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//  根节点</span></span><br><span class="line">  <span class="built_in">this</span>.root = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 方法</span></span><br><span class="line">  <span class="comment">// 插入数据</span></span><br><span class="line">  BinarySearchTree.prototype.insert = <span class="function"><span class="keyword">function</span> (<span class="params">obj</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 创建一个节点</span></span><br><span class="line">    <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(obj);</span><br><span class="line">    <span class="comment">// 先判断根节点是否存在</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">this</span>.root == <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="built_in">this</span>.root = newNode;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      insertNode(<span class="built_in">this</span>.root, newNode);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">insertNode</span>(<span class="params">currentNode, newNode</span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 向左查找</span></span><br><span class="line">      <span class="keyword">if</span> (newNode.key &lt; currentNode.key) &#123;</span><br><span class="line">        <span class="comment">// 找到 currentNode 的左子节点为 null 时放入</span></span><br><span class="line">        <span class="keyword">if</span> (currentNode.left == <span class="literal">null</span>) &#123;</span><br><span class="line">          currentNode.left = newNode;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          insertNode(currentNode.left, newNode);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 向右查找</span></span><br><span class="line">        <span class="keyword">if</span> (currentNode.right == <span class="literal">null</span>) &#123;</span><br><span class="line">          currentNode.right = newNode;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          insertNode(currentNode.right, newNode);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 树的遍历</span></span><br><span class="line">  <span class="comment">// 1.先序遍历</span></span><br><span class="line">  BinarySearchTree.prototype.preorderTraversal = <span class="function"><span class="keyword">function</span> (<span class="params">handler</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;===== 先序遍历 =====&quot;</span>);</span><br><span class="line">    preorderTraversalNode(<span class="built_in">this</span>.root);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">preorderTraversalNode</span>(<span class="params">currentNode</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// 传一个回调 handler, 让 handler 处理当前 (经过的节点) 节点的数据</span></span><br><span class="line">        handler(&#123; <span class="attr">key</span>: currentNode.key, <span class="attr">value</span>: currentNode.value &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查找子节点的左子节点</span></span><br><span class="line">        preorderTraversalNode(currentNode.left);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查找子节点的右子节点</span></span><br><span class="line">        preorderTraversalNode(currentNode.right);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 2.中序遍历</span></span><br><span class="line">  BinarySearchTree.prototype.inOrderTraversal = <span class="function"><span class="keyword">function</span> (<span class="params">handler</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;===== 中序遍历 =====&quot;</span>);</span><br><span class="line">    inOrderTraversalNode(<span class="built_in">this</span>.root);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">inOrderTraversalNode</span>(<span class="params">currentNode</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// 查找子节点的左子节点</span></span><br><span class="line">        inOrderTraversalNode(currentNode.left);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 让 handler 处理当前 (经过的节点) 节点的数据</span></span><br><span class="line">        handler(&#123; <span class="attr">key</span>: currentNode.key, <span class="attr">value</span>: currentNode.value &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查找子节点的右子节点</span></span><br><span class="line">        inOrderTraversalNode(currentNode.right);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 3.后序遍历</span></span><br><span class="line">  BinarySearchTree.prototype.postorderTraversal = <span class="function"><span class="keyword">function</span> (<span class="params">handler</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;===== 后序遍历 =====&quot;</span>);</span><br><span class="line">    postorderTraversalNode(<span class="built_in">this</span>.root);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">postorderTraversalNode</span>(<span class="params">currentNode</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// 查找子节点的左子节点</span></span><br><span class="line">        postorderTraversalNode(currentNode.left);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查找子节点的右子节点</span></span><br><span class="line">        postorderTraversalNode(currentNode.right);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 让 handler 处理当前 (经过的节点) 节点的数据</span></span><br><span class="line">        handler(&#123; <span class="attr">key</span>: currentNode.key, <span class="attr">value</span>: currentNode.value &#125;);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 寻找最值</span></span><br><span class="line">  <span class="comment">// 寻找最大值</span></span><br><span class="line">  BinarySearchTree.prototype.max = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 获取根节点</span></span><br><span class="line">    <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">    <span class="comment">// 默认 value 值</span></span><br><span class="line">    <span class="keyword">let</span> value = <span class="literal">null</span>;</span><br><span class="line">    <span class="comment">// 一直向右查找,直到节点的右子节点为 null, 当前节点就是最大值</span></span><br><span class="line">    <span class="keyword">while</span> (currentNode.right != <span class="literal">null</span>) &#123;</span><br><span class="line">      value = currentNode.value;</span><br><span class="line">      currentNode = currentNode.right;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">key</span>: currentNode.key, value &#125;;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 寻找最小值</span></span><br><span class="line">  BinarySearchTree.prototype.min = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 获取根节点</span></span><br><span class="line">    <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">    <span class="comment">// 默认 value 值</span></span><br><span class="line">    <span class="keyword">let</span> value = <span class="literal">null</span>;</span><br><span class="line">    <span class="comment">// 一直向左查找,直到节点的左子节点为 null, 当前节点就是最小值</span></span><br><span class="line">    <span class="keyword">while</span> (currentNode.left != <span class="literal">null</span>) &#123;</span><br><span class="line">      value = currentNode.value;</span><br><span class="line">      currentNode = currentNode.left;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">key</span>: currentNode.key, value &#125;;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// // 层序遍历方法 1</span></span><br><span class="line">  <span class="comment">// BinarySearchTree.prototype.levelTraversal = function () &#123;</span></span><br><span class="line">  <span class="comment">//   let currentNode = this.root;</span></span><br><span class="line">  <span class="comment">//   let result = [];</span></span><br><span class="line">  <span class="comment">//   levelTraversalNode(currentNode, 0);</span></span><br><span class="line">  <span class="comment">//   function levelTraversalNode(currentNode, level) &#123;</span></span><br><span class="line">  <span class="comment">//     if (currentNode != null) &#123;</span></span><br><span class="line">  <span class="comment">//       if (!result[level]) &#123;</span></span><br><span class="line">  <span class="comment">//         result[level] = [];</span></span><br><span class="line">  <span class="comment">//       &#125;</span></span><br><span class="line">  <span class="comment">//       result[level].push(currentNode.value);</span></span><br><span class="line">  <span class="comment">//       if (currentNode.left) &#123;</span></span><br><span class="line">  <span class="comment">//         levelTraversalNode(currentNode.left, level + 1);</span></span><br><span class="line">  <span class="comment">//       &#125;</span></span><br><span class="line">  <span class="comment">//       if (currentNode.right) &#123;</span></span><br><span class="line">  <span class="comment">//         levelTraversalNode(currentNode.right, level + 1);</span></span><br><span class="line">  <span class="comment">//       &#125;</span></span><br><span class="line">  <span class="comment">//     &#125;</span></span><br><span class="line">  <span class="comment">//   &#125;</span></span><br><span class="line">  <span class="comment">//   return result;</span></span><br><span class="line">  <span class="comment">// &#125;;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 层序遍历方法 2</span></span><br><span class="line">  BinarySearchTree.prototype.levelTraversal = <span class="function"><span class="keyword">function</span> <span class="title">levelTraversal</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> result = []; <span class="comment">// 最后输出的多维数组</span></span><br><span class="line">    <span class="keyword">let</span> length = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">let</span> item = <span class="keyword">new</span> Node();</span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">this</span>.root) &#123;</span><br><span class="line">      <span class="keyword">return</span> result;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">let</span> layer = []; <span class="comment">// 临时数组, 存放某一层的节点</span></span><br><span class="line">      layer.push(<span class="built_in">this</span>.root); <span class="comment">// 把根节点作为第一层放进数组</span></span><br><span class="line">      <span class="keyword">while</span> (layer.length != <span class="number">0</span>) &#123;</span><br><span class="line">        result.push([]); <span class="comment">// 添加新的一层</span></span><br><span class="line">        length = layer.length; <span class="comment">// 下面 shift 会改变 layer 数组长度, 所以先保存下</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; length; i++) &#123;</span><br><span class="line">          item = layer.shift(); <span class="comment">// 层虚遍历从左到右</span></span><br><span class="line">          result[result.length - <span class="number">1</span>].push(item.key); <span class="comment">// 把这个节点的值, 添加到当前层的数组里面</span></span><br><span class="line">          <span class="comment">// 将下一层的节点加入 layer</span></span><br><span class="line">          <span class="keyword">if</span> (item.left) &#123;</span><br><span class="line">            layer.push(item.left);</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">if</span> (item.right) &#123;</span><br><span class="line">            layer.push(item.right);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 搜索 key 值</span></span><br><span class="line">  <span class="comment">// BinarySearchTree.prototype.search = function (key) &#123;</span></span><br><span class="line">  <span class="comment">//   return searchNode(this.root, key);</span></span><br><span class="line">  <span class="comment">//   function searchNode(currentNode, key) &#123;</span></span><br><span class="line">  <span class="comment">//     if (currentNode == null) &#123;</span></span><br><span class="line">  <span class="comment">//       return false;</span></span><br><span class="line">  <span class="comment">//     &#125; else &#123;</span></span><br><span class="line">  <span class="comment">//       if (key &lt; currentNode.key) &#123;</span></span><br><span class="line">  <span class="comment">//         return searchNode(currentNode.left, key);</span></span><br><span class="line">  <span class="comment">//       &#125; else if (key &gt; currentNode.key) &#123;</span></span><br><span class="line">  <span class="comment">//         return searchNode(currentNode.right, key);</span></span><br><span class="line">  <span class="comment">//       &#125; else &#123;</span></span><br><span class="line">  <span class="comment">//         return true;</span></span><br><span class="line">  <span class="comment">//       &#125;</span></span><br><span class="line">  <span class="comment">//     &#125;</span></span><br><span class="line">  <span class="comment">//   &#125;</span></span><br><span class="line">  <span class="comment">// &#125;;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 搜索 key 值 ( 非递归式 )</span></span><br><span class="line">  BinarySearchTree.prototype.search = <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">    <span class="keyword">while</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (key &lt; currentNode.key) &#123;</span><br><span class="line">        currentNode = currentNode.left;</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (key &gt; currentNode.key) &#123;</span><br><span class="line">        currentNode = currentNode.right;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 删除节点</span></span><br><span class="line">  BinarySearchTree.prototype.remove = <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!key) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="comment">// 先寻找要删除的节点</span></span><br><span class="line">    <span class="comment">// 涉及到当前节点和父节点, 以及具体关系 ( 是左子节点还是右子节点 )</span></span><br><span class="line">    <span class="keyword">let</span> parentNode = <span class="built_in">this</span>.root;</span><br><span class="line">    <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">    <span class="keyword">let</span> isLeftChild = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (key != currentNode.key) &#123;</span><br><span class="line">      parentNode = currentNode;</span><br><span class="line">      <span class="keyword">if</span> (key &lt; currentNode.key) &#123;</span><br><span class="line">        isLeftChild = <span class="literal">true</span>;</span><br><span class="line">        currentNode = currentNode.left;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        isLeftChild = <span class="literal">false</span>;</span><br><span class="line">        currentNode = currentNode.right;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 没找到要删除的节点</span></span><br><span class="line">      <span class="keyword">if</span> (!currentNode) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// while 循环结束, 已找到要删除的节点, 下面分几种情况</span></span><br><span class="line">    <span class="comment">// 1. 要删除的节点是叶子节点</span></span><br><span class="line">    <span class="keyword">if</span> (currentNode.left == <span class="literal">null</span> &amp;&amp; currentNode.right == <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) &#123;</span><br><span class="line">        <span class="built_in">this</span>.root = <span class="literal">null</span>;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 判断当前节点和父节点的关系</span></span><br><span class="line">        <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">          parentNode.left = <span class="literal">null</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          parentNode.right = <span class="literal">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 2. 要删除的节点只有一个子节点 ( 只有左子节点 )</span></span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (!currentNode.right) &#123;</span><br><span class="line">      <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) &#123;</span><br><span class="line">        <span class="built_in">this</span>.root == currentNode.left;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 判断当前节点和父节点的关系</span></span><br><span class="line">      <span class="keyword">else</span> <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">        parentNode.left = currentNode.left;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        parentNode.right = currentNode.left;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 3. 要删除的节点只有一个子节点 ( 只有右子节点 )</span></span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (!currentNode.left) &#123;</span><br><span class="line">      <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) &#123;</span><br><span class="line">        <span class="built_in">this</span>.root == currentNode.left;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 判断当前节点和父节点的关系</span></span><br><span class="line">      <span class="keyword">else</span> <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">        parentNode.left = currentNode.right;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        parentNode.right = currentNode.right;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 4. 要删除的节点有两个子节点</span></span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 通过找规律发现, 该节点即在左子树中找最大的值 或者 在右子树中找最小的值</span></span><br><span class="line">      <span class="comment">// By the way, it&#x27;s fucking complicated !</span></span><br><span class="line">      <span class="comment">// 有两种方案, 这里只展示向删除节点的左子树方向查找</span></span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) <span class="built_in">this</span>.root = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">let</span> candidateNode = currentNode.left; <span class="comment">// 初始候选替补节点</span></span><br><span class="line">      <span class="keyword">let</span> candidateParentNode = currentNode.left; <span class="comment">// 初始候选替补节点的父节点</span></span><br><span class="line">      <span class="comment">// 寻找合适的节点</span></span><br><span class="line">      <span class="keyword">while</span> (candidateNode.right) &#123;</span><br><span class="line">        candidateParentNode = candidateNode;</span><br><span class="line">        candidateNode = candidateNode.right;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 如果候选节点没有右子树, 则当前节点就是最终替补节点</span></span><br><span class="line">      <span class="keyword">if</span> (candidateNode == candidateParentNode) &#123;</span><br><span class="line">        candidateNode.right = currentNode.right;</span><br><span class="line">        <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">          parentNode.left = candidateNode;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          parentNode.right = candidateNode;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 如果候选节点有右子树</span></span><br><span class="line">      <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 再判断是否有左子树 (隔代)</span></span><br><span class="line">        <span class="keyword">if</span> (candidateNode.left) &#123;</span><br><span class="line">          candidateParentNode.right = candidateNode.left;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          candidateParentNode.right = <span class="literal">null</span>;</span><br><span class="line">          candidateNode.left = currentNode.left;</span><br><span class="line">          candidateNode.right = currentNode.right;</span><br><span class="line">          <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">            parentNode.left = candidateNode;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            parentNode.right = candidateNode;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 测试</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> binarySearchTree = <span class="keyword">new</span> BinarySearchTree();</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">11</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 11 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">7</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 7 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">15</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 15 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">5</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 5 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">3</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 3 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">9</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 9 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">8</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 8 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">10</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 10 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">13</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 13 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">12</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 12 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">14</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 14 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">20</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 20 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">18</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 18 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">25</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 25 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">19</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 19 !&quot;</span> &#125;);</span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//                            11</span></span><br><span class="line"><span class="comment">//                  7                     15</span></span><br><span class="line"><span class="comment">//            5         9             13      20</span></span><br><span class="line"><span class="comment">//        3           8  10         12 14   18   25</span></span><br><span class="line"><span class="comment">//                                           19</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 先序</span></span><br><span class="line"><span class="comment">// binarySearchTree.preorderTraversal(function (value) &#123;</span></span><br><span class="line"><span class="comment">//   console.log(value);</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"><span class="comment">// 中序</span></span><br><span class="line"><span class="comment">// binarySearchTree.inOrderTraversal(function (value) &#123;</span></span><br><span class="line"><span class="comment">//   console.log(value);</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"><span class="comment">// 后序</span></span><br><span class="line"><span class="comment">// binarySearchTree.postorderTraversal(function (value) &#123;</span></span><br><span class="line"><span class="comment">//   console.log(value);</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"><span class="comment">// 层序</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.levelTraversal());</span></span><br><span class="line"><span class="comment">// 最大值</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.max());</span></span><br><span class="line"><span class="comment">// 最小值</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.min());</span></span><br><span class="line"><span class="comment">// 搜索 key</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(11));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(7));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(15));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(5));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(3));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(9));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(8));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(10));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(6));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(13));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(12));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(14));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(20));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(18));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(25));</span></span><br><span class="line"><span class="built_in">console</span>.log(binarySearchTree.levelTraversal());</span><br><span class="line"><span class="built_in">console</span>.log(binarySearchTree.remove(<span class="number">11</span>));</span><br><span class="line"><span class="built_in">console</span>.log(binarySearchTree.levelTraversal());</span><br></pre></td></tr></table></figure>
</div></div>

<hr>
<h1 id="创建二叉搜索树这个类"><a href="#创建二叉搜索树这个类" class="headerlink" title="创建二叉搜索树这个类"></a>创建二叉搜索树这个类</h1><p><strong>顺便定义根节点, 先让它为空</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">BinarySearchTree</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.root = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="创建节点类-用于承载每个节点"><a href="#创建节点类-用于承载每个节点" class="headerlink" title="创建节点类, 用于承载每个节点"></a>创建节点类, 用于承载每个节点</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Node</span>(<span class="params">obj = &#123;&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.key = obj.key;</span><br><span class="line">  <span class="built_in">this</span>.value = obj.value;</span><br><span class="line">  <span class="built_in">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">  <span class="built_in">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="封装二叉搜索树的一些方法"><a href="#封装二叉搜索树的一些方法" class="headerlink" title="封装二叉搜索树的一些方法"></a>封装二叉搜索树的一些方法</h1><h2 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 插入数据</span></span><br><span class="line">BinarySearchTree.prototype.insertNode = <span class="function"><span class="keyword">function</span> (<span class="params">obj</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 创建一个节点</span></span><br><span class="line">  <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(obj);</span><br><span class="line">  <span class="comment">// 先判断根节点是否存在</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">this</span>.root == <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.root = newNode;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    insert(<span class="built_in">this</span>.root, newNode);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">insert</span>(<span class="params">currentNode, newNode</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 向左查找</span></span><br><span class="line">    <span class="keyword">if</span> (newNode.key &lt; currentNode.key) &#123;</span><br><span class="line">      <span class="comment">// 找到 currentNode 的左子节点为 null 时放入</span></span><br><span class="line">      <span class="keyword">if</span> (currentNode.left == <span class="literal">null</span>) &#123;</span><br><span class="line">        currentNode.left = newNode;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        insert(currentNode.left, newNode);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 向右查找</span></span><br><span class="line">      <span class="keyword">if</span> (currentNode.right == <span class="literal">null</span>) &#123;</span><br><span class="line">        currentNode.right = newNode;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        insert(currentNode.right, newNode);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="先序遍历"><a href="#先序遍历" class="headerlink" title="先序遍历"></a>先序遍历</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 先序遍历</span></span><br><span class="line">BinarySearchTree.prototype.preorderTraversal = <span class="function"><span class="keyword">function</span> (<span class="params">handler</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;===== 先序遍历 =====&quot;</span>);</span><br><span class="line">  preorderTraversalNode(<span class="built_in">this</span>.root);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">preorderTraversalNode</span>(<span class="params">currentNode</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="comment">// 传一个回调 handler, 让 handler 处理当前 (经过的节点) 节点的数据</span></span><br><span class="line">      handler(&#123; <span class="attr">key</span>: currentNode.key, <span class="attr">value</span>: currentNode.value &#125;);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 查找子节点的左子节点</span></span><br><span class="line">      preorderTraversalNode(currentNode.left);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 查找子节点的右子节点</span></span><br><span class="line">      preorderTraversalNode(currentNode.right);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="中序遍历"><a href="#中序遍历" class="headerlink" title="中序遍历"></a>中序遍历</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 中序遍历</span></span><br><span class="line">BinarySearchTree.prototype.inOrderTraversal = <span class="function"><span class="keyword">function</span> (<span class="params">handler</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;===== 中序遍历 =====&quot;</span>);</span><br><span class="line">  inOrderTraversalNode(<span class="built_in">this</span>.root);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">inOrderTraversalNode</span>(<span class="params">currentNode</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="comment">// 查找子节点的左子节点</span></span><br><span class="line">      inOrderTraversalNode(currentNode.left);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 让 handler 处理当前 (经过的节点) 节点的数据</span></span><br><span class="line">      handler(&#123; <span class="attr">key</span>: currentNode.key, <span class="attr">value</span>: currentNode.value &#125;);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 查找子节点的右子节点</span></span><br><span class="line">      inOrderTraversalNode(currentNode.right);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="后序遍历"><a href="#后序遍历" class="headerlink" title="后序遍历"></a>后序遍历</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 后序遍历</span></span><br><span class="line">BinarySearchTree.prototype.postorderTraversal = <span class="function"><span class="keyword">function</span> (<span class="params">handler</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;===== 后序遍历 =====&quot;</span>);</span><br><span class="line">  postorderTraversalNode(<span class="built_in">this</span>.root);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">postorderTraversalNode</span>(<span class="params">currentNode</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="comment">// 查找子节点的左子节点</span></span><br><span class="line">      postorderTraversalNode(currentNode.left);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 查找子节点的右子节点</span></span><br><span class="line">      postorderTraversalNode(currentNode.right);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 让 handler 处理当前 (经过的节点) 节点的数据</span></span><br><span class="line">      handler(&#123; <span class="attr">key</span>: currentNode.key, <span class="attr">value</span>: currentNode.value &#125;);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="寻找最大值"><a href="#寻找最大值" class="headerlink" title="寻找最大值"></a>寻找最大值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 寻找最大值</span></span><br><span class="line">BinarySearchTree.prototype.max = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 获取根节点</span></span><br><span class="line">  <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">  <span class="comment">// 默认 value 值</span></span><br><span class="line">  <span class="keyword">let</span> value = <span class="literal">null</span>;</span><br><span class="line">  <span class="comment">// 一直向右查找,直到节点的右子节点为 null, 当前节点就是最大值</span></span><br><span class="line">  <span class="keyword">while</span> (currentNode.right != <span class="literal">null</span>) &#123;</span><br><span class="line">    value = currentNode.value;</span><br><span class="line">    currentNode = currentNode.right;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; <span class="attr">key</span>: currentNode.key, value &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="寻找最小值"><a href="#寻找最小值" class="headerlink" title="寻找最小值"></a>寻找最小值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 寻找最小值</span></span><br><span class="line">BinarySearchTree.prototype.min = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 获取根节点</span></span><br><span class="line">  <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">  <span class="comment">// 默认 value 值</span></span><br><span class="line">  <span class="keyword">let</span> value = <span class="literal">null</span>;</span><br><span class="line">  <span class="comment">// 一直向左查找,直到节点的左子节点为 null, 当前节点就是最小值</span></span><br><span class="line">  <span class="keyword">while</span> (currentNode.left != <span class="literal">null</span>) &#123;</span><br><span class="line">    value = currentNode.value;</span><br><span class="line">    currentNode = currentNode.left;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; <span class="attr">key</span>: currentNode.key, value &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="层序遍历"><a href="#层序遍历" class="headerlink" title="层序遍历"></a>层序遍历</h2><h3 id="层序遍历方法-1"><a href="#层序遍历方法-1" class="headerlink" title="层序遍历方法 1"></a>层序遍历方法 1</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 层序遍历方法 1</span></span><br><span class="line">BinarySearchTree.prototype.levelTraversal = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">  <span class="keyword">let</span> result = [];</span><br><span class="line">  levelTraversalNode(currentNode, <span class="number">0</span>);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">levelTraversalNode</span>(<span class="params">currentNode, level</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (!result[level]) &#123;</span><br><span class="line">        result[level] = [];</span><br><span class="line">      &#125;</span><br><span class="line">      result[level].push(currentNode.value);</span><br><span class="line">      <span class="keyword">if</span> (currentNode.left) &#123;</span><br><span class="line">        levelTraversalNode(currentNode.left, level + <span class="number">1</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (currentNode.right) &#123;</span><br><span class="line">        levelTraversalNode(currentNode.right, level + <span class="number">1</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="层序遍历方法-2"><a href="#层序遍历方法-2" class="headerlink" title="层序遍历方法 2"></a>层序遍历方法 2</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">BinarySearchTree.prototype.levelTraversal = <span class="function"><span class="keyword">function</span> <span class="title">levelTraversal</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> result = []; <span class="comment">// 最后输出的结构 (多维数组)</span></span><br><span class="line">  <span class="keyword">let</span> length = <span class="number">0</span>; <span class="comment">// layer 数组长度; 声明在外面是为了尽量不在 while 内部使用 let</span></span><br><span class="line">  <span class="keyword">let</span> item = &#123;&#125;; <span class="comment">// layer 数组里的每一个节点; 声明在外面是为了尽量不在 while 内部使用 let</span></span><br><span class="line">  <span class="keyword">if</span> (!<span class="built_in">this</span>.root) &#123;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> layer = []; <span class="comment">// 临时数组, 存放某一层的节点</span></span><br><span class="line">    layer.push(<span class="built_in">this</span>.root); <span class="comment">// 把根节点作为第一层放进数组</span></span><br><span class="line">    <span class="keyword">while</span> (layer.length != <span class="number">0</span>) &#123;</span><br><span class="line">      result.push([]); <span class="comment">// 添加新的一层</span></span><br><span class="line">      length = layer.length; <span class="comment">// 下面 shift() 会改变 layer 数组长度, 所以先保存下来</span></span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; length; i++) &#123;</span><br><span class="line">        item = layer.shift(); <span class="comment">// 层虚遍历从左到右</span></span><br><span class="line">        result[result.length - <span class="number">1</span>].push(item.key); <span class="comment">// 把这个节点的值, 添加到当前层的数组里面</span></span><br><span class="line">        <span class="comment">// 将下一层的节点加入 layer</span></span><br><span class="line">        <span class="keyword">if</span> (item.left) &#123;</span><br><span class="line">          layer.push(item.left);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (item.right) &#123;</span><br><span class="line">          layer.push(item.right);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="搜索"><a href="#搜索" class="headerlink" title="搜索"></a>搜索</h2><h3 id="搜索-递归式"><a href="#搜索-递归式" class="headerlink" title="搜索 ( 递归式 )"></a>搜索 ( 递归式 )</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 搜索 key 值</span></span><br><span class="line">BinarySearchTree.prototype.search = <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> searchNode(<span class="built_in">this</span>.root, key);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">searchNode</span>(<span class="params">currentNode, key</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode == <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (key &lt; currentNode.key) &#123;</span><br><span class="line">        <span class="keyword">return</span> searchNode(currentNode.left, key);</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (key &gt; currentNode.key) &#123;</span><br><span class="line">        <span class="keyword">return</span> searchNode(currentNode.right, key);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="搜索-非递归式"><a href="#搜索-非递归式" class="headerlink" title="搜索 ( 非递归式 )"></a>搜索 ( 非递归式 )</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 搜索 key 值 ( 非递归式 )</span></span><br><span class="line">BinarySearchTree.prototype.search = <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">  <span class="keyword">while</span> (currentNode != <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (key &lt; currentNode.key) &#123;</span><br><span class="line">      currentNode = currentNode.left;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (key &gt; currentNode.key) &#123;</span><br><span class="line">      currentNode = currentNode.right;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 删除节点</span></span><br><span class="line">BinarySearchTree.prototype.remove = <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!key) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  <span class="comment">// 先寻找要删除的节点</span></span><br><span class="line">  <span class="comment">// 涉及到当前节点和父节点, 以及具体关系 ( 是左子节点还是右子节点 )</span></span><br><span class="line">  <span class="keyword">let</span> parentNode = <span class="built_in">this</span>.root;</span><br><span class="line">  <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.root;</span><br><span class="line">  <span class="keyword">let</span> isLeftChild = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">while</span> (key != currentNode.key) &#123;</span><br><span class="line">    parentNode = currentNode;</span><br><span class="line">    <span class="keyword">if</span> (key &lt; currentNode.key) &#123;</span><br><span class="line">      isLeftChild = <span class="literal">true</span>;</span><br><span class="line">      currentNode = currentNode.left;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      isLeftChild = <span class="literal">false</span>;</span><br><span class="line">      currentNode = currentNode.right;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 没找到要删除的节点</span></span><br><span class="line">    <span class="keyword">if</span> (!currentNode) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// while 循环结束, 已找到要删除的节点, 下面分几种情况</span></span><br><span class="line">  <span class="comment">// 1. 要删除的节点是叶子节点</span></span><br><span class="line">  <span class="keyword">if</span> (currentNode.left == <span class="literal">null</span> &amp;&amp; currentNode.right == <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) &#123;</span><br><span class="line">      <span class="built_in">this</span>.root = <span class="literal">null</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 判断当前节点和父节点的关系</span></span><br><span class="line">      <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">        parentNode.left = <span class="literal">null</span>;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        parentNode.right = <span class="literal">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 2. 要删除的节点只有一个子节点 ( 只有左子节点 )</span></span><br><span class="line">  <span class="keyword">else</span> <span class="keyword">if</span> (!currentNode.right) &#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) &#123;</span><br><span class="line">      <span class="built_in">this</span>.root == currentNode.left;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断当前节点和父节点的关系</span></span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">      parentNode.left = currentNode.left;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      parentNode.right = currentNode.left;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 3. 要删除的节点只有一个子节点 ( 只有右子节点 )</span></span><br><span class="line">  <span class="keyword">else</span> <span class="keyword">if</span> (!currentNode.left) &#123;</span><br><span class="line">    <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) &#123;</span><br><span class="line">      <span class="built_in">this</span>.root == currentNode.left;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断当前节点和父节点的关系</span></span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">      parentNode.left = currentNode.right;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      parentNode.right = currentNode.right;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 4. 要删除的节点有两个子节点</span></span><br><span class="line">  <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 通过找规律发现, 该节点即在左子树中找最大的值 或者 在右子树中找最小的值</span></span><br><span class="line">    <span class="comment">// By the way, it&#x27;s fucking complicated !</span></span><br><span class="line">    <span class="comment">// 有两种方案, 这里只展示向删除节点的左子树方向查找</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (currentNode == <span class="built_in">this</span>.root) <span class="built_in">this</span>.root = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> candidateNode = currentNode.left; <span class="comment">// 初始候选替补节点</span></span><br><span class="line">    <span class="keyword">let</span> candidateParentNode = currentNode.left; <span class="comment">// 初始候选替补节点的父节点</span></span><br><span class="line">    <span class="comment">// 寻找合适的节点</span></span><br><span class="line">    <span class="keyword">while</span> (candidateNode.right) &#123;</span><br><span class="line">      candidateParentNode = candidateNode;</span><br><span class="line">      candidateNode = candidateNode.right;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果候选节点没有右子树, 则当前节点就是最终替补节点</span></span><br><span class="line">    <span class="keyword">if</span> (candidateNode == candidateParentNode) &#123;</span><br><span class="line">      candidateNode.right = currentNode.right;</span><br><span class="line">      <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">        parentNode.left = candidateNode;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        parentNode.right = candidateNode;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果候选节点有右子树</span></span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 再判断是否有左子树 (隔代)</span></span><br><span class="line">      <span class="keyword">if</span> (candidateNode.left) &#123;</span><br><span class="line">        candidateParentNode.right = candidateNode.left;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        candidateParentNode.right = <span class="literal">null</span>;</span><br><span class="line">        candidateNode.left = currentNode.left;</span><br><span class="line">        candidateNode.right = currentNode.right;</span><br><span class="line">        <span class="keyword">if</span> (isLeftChild) &#123;</span><br><span class="line">          parentNode.left = candidateNode;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          parentNode.right = candidateNode;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 测试</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> binarySearchTree = <span class="keyword">new</span> BinarySearchTree();</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">11</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 11 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">7</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 7 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">15</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 15 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">5</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 5 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">3</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 3 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">9</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 9 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">8</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 8 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">10</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 10 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">13</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 13 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">12</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 12 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">14</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 14 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">20</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 20 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">18</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 18 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">25</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 25 !&quot;</span> &#125;);</span><br><span class="line">binarySearchTree.insert(&#123; <span class="attr">key</span>: <span class="number">19</span>, <span class="attr">value</span>: <span class="string">&quot;I&#x27;m 19 !&quot;</span> &#125;);</span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//                            11</span></span><br><span class="line"><span class="comment">//                  7                     15</span></span><br><span class="line"><span class="comment">//            5         9             13      20</span></span><br><span class="line"><span class="comment">//        3           8  10         12 14   18   25</span></span><br><span class="line"><span class="comment">//                                           19</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 先序</span></span><br><span class="line"><span class="comment">// binarySearchTree.preorderTraversal(function (value) &#123;</span></span><br><span class="line"><span class="comment">//   console.log(value);</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"><span class="comment">// 中序</span></span><br><span class="line"><span class="comment">// binarySearchTree.inOrderTraversal(function (value) &#123;</span></span><br><span class="line"><span class="comment">//   console.log(value);</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"><span class="comment">// 后序</span></span><br><span class="line"><span class="comment">// binarySearchTree.postorderTraversal(function (value) &#123;</span></span><br><span class="line"><span class="comment">//   console.log(value);</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"><span class="comment">// 层序</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.levelTraversal());</span></span><br><span class="line"><span class="comment">// 最大值</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.max());</span></span><br><span class="line"><span class="comment">// 最小值</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.min());</span></span><br><span class="line"><span class="comment">// 搜索 key</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(11));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(7));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(15));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(5));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(3));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(9));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(8));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(10));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(6));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(13));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(12));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(14));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(20));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(18));</span></span><br><span class="line"><span class="comment">// console.log(binarySearchTree.search(25));</span></span><br><span class="line"><span class="built_in">console</span>.log(binarySearchTree.levelTraversal());</span><br><span class="line"><span class="built_in">console</span>.log(binarySearchTree.remove(<span class="number">11</span>));</span><br><span class="line"><span class="built_in">console</span>.log(binarySearchTree.levelTraversal());</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">HHR</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://codehhr.cn/2021/10/19/data-structure/ds-binarySearchTree/">https://codehhr.cn/2021/10/19/data-structure/ds-binarySearchTree/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://codehhr.cn" target="_blank">Saturday_</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a><a class="post-meta__tags" href="/tags/%E4%BA%8C%E5%8F%89%E6%A0%91/">二叉树</a></div><div class="post_share"><div class="social-share" data-image="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/11/01/data-structure/ds-doublyLinkedList/"><img class="prev-cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Js 封装双向链表</div></div></a></div><div class="next-post pull-right"><a href="/2021/10/15/js/js-object-create/"><img class="next-cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">用 Object.create 实现类式继承</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/11/01/data-structure/ds-doublyLinkedList/" title="Js 封装双向链表"><img class="cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-11-05</div><div class="title">Js 封装双向链表</div></div></a></div><div><a href="/2021/10/12/data-structure/ds-linkedList/" title="用 Js 封装单链表"><img class="cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-10-20</div><div class="title">用 Js 封装单链表</div></div></a></div><div><a href="/2021/10/12/data-structure/ds-passflower/" title="面试题-击鼓传花 ( 队列 )"><img class="cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-10-20</div><div class="title">面试题-击鼓传花 ( 队列 )</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="avatar"/></div><div class="author-info__name">HHR</div><div class="author-info__description">Linux is great ~</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">87</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">120</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/codehhr"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/codehhr" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1871973389@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">评论关了, 图床也挂了</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E5%B0%81%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">二叉搜索树的封装</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="toc-number">2.</span> <span class="toc-text">完整代码</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%BF%99%E4%B8%AA%E7%B1%BB"><span class="toc-number">3.</span> <span class="toc-text">创建二叉搜索树这个类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E8%8A%82%E7%82%B9%E7%B1%BB-%E7%94%A8%E4%BA%8E%E6%89%BF%E8%BD%BD%E6%AF%8F%E4%B8%AA%E8%8A%82%E7%82%B9"><span class="toc-number">4.</span> <span class="toc-text">创建节点类, 用于承载每个节点</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B0%81%E8%A3%85%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E4%B8%80%E4%BA%9B%E6%96%B9%E6%B3%95"><span class="toc-number">5.</span> <span class="toc-text">封装二叉搜索树的一些方法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE"><span class="toc-number">5.1.</span> <span class="toc-text">插入数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="toc-number">5.2.</span> <span class="toc-text">先序遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="toc-number">5.3.</span> <span class="toc-text">中序遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="toc-number">5.4.</span> <span class="toc-text">后序遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%BB%E6%89%BE%E6%9C%80%E5%A4%A7%E5%80%BC"><span class="toc-number">5.5.</span> <span class="toc-text">寻找最大值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%BB%E6%89%BE%E6%9C%80%E5%B0%8F%E5%80%BC"><span class="toc-number">5.6.</span> <span class="toc-text">寻找最小值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86"><span class="toc-number">5.7.</span> <span class="toc-text">层序遍历</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86%E6%96%B9%E6%B3%95-1"><span class="toc-number">5.7.1.</span> <span class="toc-text">层序遍历方法 1</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86%E6%96%B9%E6%B3%95-2"><span class="toc-number">5.7.2.</span> <span class="toc-text">层序遍历方法 2</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2"><span class="toc-number">5.8.</span> <span class="toc-text">搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2-%E9%80%92%E5%BD%92%E5%BC%8F"><span class="toc-number">5.8.1.</span> <span class="toc-text">搜索 ( 递归式 )</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2-%E9%9D%9E%E9%80%92%E5%BD%92%E5%BC%8F"><span class="toc-number">5.8.2.</span> <span class="toc-text">搜索 ( 非递归式 )</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9"><span class="toc-number">5.9.</span> <span class="toc-text">删除节点</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95"><span class="toc-number">6.</span> <span class="toc-text">测试</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/01/17/linux/linux-grub/" title="linux grub 恢复"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="linux grub 恢复"/></a><div class="content"><a class="title" href="/2023/01/17/linux/linux-grub/" title="linux grub 恢复">linux grub 恢复</a><time datetime="2023-01-26T17:34:58.993Z" title="更新于 2023-01-27 01:34:58">2023-01-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/08/ts/ts-dataType/" title="Ts 学习总结"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/ts/ts.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="Ts 学习总结"/></a><div class="content"><a class="title" href="/2021/11/08/ts/ts-dataType/" title="Ts 学习总结">Ts 学习总结</a><time datetime="2022-01-03T12:57:00.880Z" title="更新于 2022-01-03 20:57:00">2022-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/09/15/ts/ts-install-typescript/" title="TypeScript 环境安装"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/cover/tree.png" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="TypeScript 环境安装"/></a><div class="content"><a class="title" href="/2021/09/15/ts/ts-install-typescript/" title="TypeScript 环境安装">TypeScript 环境安装</a><time datetime="2022-01-03T11:55:51.110Z" title="更新于 2022-01-03 19:55:51">2022-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/29/js/js-common-require/" title="关于 commonjs &amp; requirejs"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="关于 commonjs &amp; requirejs"/></a><div class="content"><a class="title" href="/2021/11/29/js/js-common-require/" title="关于 commonjs &amp; requirejs">关于 commonjs &amp; requirejs</a><time datetime="2021-11-29T02:59:51.798Z" title="更新于 2021-11-29 10:59:51">2021-11-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/18/vue/vue-build-optimization/" title="Vue 打包优化"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/vue/vue.jpg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="Vue 打包优化"/></a><div class="content"><a class="title" href="/2021/11/18/vue/vue-build-optimization/" title="Vue 打包优化">Vue 打包优化</a><time datetime="2021-11-18T16:14:49.447Z" title="更新于 2021-11-19 00:14:49">2021-11-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By HHR</div><div class="footer_custom_text">In case I don’t see you ... Good afternoon,good evening,and good night.<p><a target="_blank" rel="noopener" href='https://beian.miit.gov.cn/' style='color:#00CCFF'>冀ICP备2020027302号-1</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-moon"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="https://js.exj-0224.cc/slot?7255774253627174051-11305"></script><script src="/js/onload.js"></script><script src="/js/artitalk.js"></script><script src="/js/formatted.js"></script><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script src="/js/jquery.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>